var componentinfo= ["./component/tab/avalon.tab","./component/dropdownmenu/avalon.dropdownmenu"];
var newjson = [{title:"一级菜单1" ,state:"home",icon:"home_icon", open:true,innermenuinfo:[
    {title:"百度",state:"firstview",src:"http://www.baidu.com/",icon:"text"},
    {title:"豆瓣",state:"secondview",src:"https://www.douban.com/",icon:"text"},
    {title:"遨游",state:"thirdview",src:"http://www.aoyou.com/",icon:"text"}
]},
    {title:"一级菜单2",state:"home",icon:"home_icon", open:true,innermenuinfo:[
        {title:"ipc.me",state:"firstview",src:"http://www.ipc.me/",icon:"text"}
    ]}
];

var menuinfo = '<ul class="aoyou-tab-menu" ms-visible="menushow" ms-css-left="menuleft" ms-css-top="menutop">'+
    '    <li class="aoyou-tab-menu-bottomborder" ms-click="menurefresh">刷新</li>'+
    '    <li ms-click="menuclose($event)">关闭</li>'+
    '    <li ms-click="menucloseall($event)">全部关闭</li>'+
    '    <li ms-click="menucloseother($event)" class="aoyou-tab-menu-bottomborder">除此之外全部关闭</li>'+
    '    <li ms-click="menucloseright($event)">当前页右侧全部关闭</li>'+
    '    <li ms-click="menucloseleft($event)">当前页左侧全部关闭</li>'+
    '</ul>';

require(componentinfo, function () {
    var vm = avalon.define({
        $id: "test",
        currPath:"",

        titleshow:true,
        $dropdownmenuconfig1:{
            mainiconclass:'user',
            outerclass:'top_content_user',
            data:[{content:"消息查看",href:"#",icontype:"mail"},{content:"修改密码",href:"#",icontype:"key"},{content:"退出",href:"#",icontype:"logout"}]
        },
        $dropdownmenuconfig2:{
            messagenum:90,
            mainiconclass:'mail',
            outerclass:'top_content_mail',
            data:[{content:"系统消息",href:"#",messagenum:'80'},{content:"私信",href:"#",messagenum:'10'}]
        },
        frametab:{
            active:0,//必须提前设定默认值否则更改无效
            target:"framename",
            removable: true,
            event:"mousedown",
            nextEnable:1,
            prevEnable:1,
            menushow:false,
            menuleft:0,
            menutop:0,
            menu:menuinfo,
            //刷新按钮
            onmenurefresh:function ($event,vvmm) {
                window.open(document.all.framename.src,'framename','');
            },
            //关闭按钮 关闭页面部分
            onmenuclose:function ($event,vvmm) {
                vvmm.menushow = false;
                //$('#frameid').attr('src',obj.$model.href);
            },
            onmenucloseall:function ($event,vvmm) {
                vvmm.menushow = false;
                $('#frameid').attr('src','');
            },
            onSlider:function(obj,idx){
                //新增——左右箭头滑动
                $('#frameid').attr('src',obj.$model.href);
            },
            afterRemove:function(obj,idx){//删除某个tab之后跳转回前一个页面
                $('#frameid').attr('src',obj.$model.href);
            },
            onRightMouse:function (event,index,vvmm) {
                //判断鼠标点击事件,2为鼠标右键,弹出右键相应菜单
                vvmm.menushow = true;
                vvmm.menuleft = event.pageX-215;
                vvmm.menutop = event.pageY-50;
                $('#frameid').attr('src',event.target.href);
            },
            tabs:[{title: "百度",href: "http://www.baidu.com/",iconclass:"text",iconshow:true,removable: false}]
        },
        menuinfo1:newjson,
        openinner:function(i){
            if(vm.titleshow){
                vm.menuinfo1[i].open = !vm.menuinfo1[i].open;
            }
        },
        //菜单展开
        controlmenu:function(){
            if(vm.menushow){
                vm.titleshow = false;
                avalon.each(vm.menuinfo1,function(o,e){
                    e.open = false;
                });
                $('.menu').animate({
                    width: "45px",
                    overflowX:"hidden",
                    overflowY:"hidden"
                }, 500 );
                $('.main').animate({
                    left: "45px"
                }, 500);
            }else{
                $('.menu').animate({
                    width: "218px"
                }, 500 );
                $('.main').animate({
                    left: "218px"
                }, 500,null,function(){
                    vm.titleshow = true;
                });
            }
            vm.menushow = !vm.menushow;
        },
        pageJump:function(_title,_src,_icon){

            var frameTab = avalon.vmodels['frameTab'];
            var index =-1;
            if(frameTab){
                var ts = frameTab.tabs;
                if(ts){
                    for(var i=0,l=ts.$model.length;i<l;i++){
                        if(ts.$model[i].href == _src){
                            index = i;
                            break;
                        }
                    }
                }
                tabOpt.name = _title;
                tabOpt.title = _title;
                tabOpt.href = _src;
                if(_icon){
                    tabOpt.iconshow = true;
                    tabOpt.iconclass = _icon;
                }
                if(index == -1){
                    frameTab.add(tabOpt);
                    frameTab.active = ts.$model.length-1;
                }else{
                    frameTab.active = index;
                }
                //新增——更新iframe的src
                $('#frameid').attr('src',_src)
            }
        }
    });
    var tabOpt = {
        target:"_self",
        removable: true,
        event:"mousedown"
    }
    var _obj = [];
    var _temp = newjson.$model;
    for(var i=0,l=_temp.length;i<l;i++){
        if(_temp[i].innermenuinfo != undefined && _temp[i].innermenuinfo.length>0){
            for(var z in _temp[i].innermenuinfo){
                _obj.push((_temp[i].innermenuinfo)[z]);
            }
        }
    }

    var vms = [vm];//加载各个页面的VM
    for(var i=0,l = arguments.length;i<l;i++){
        if(arguments[i] && arguments[i].$id){
            vms.push(arguments[i]);
        }
    }
    avalon.scan(document.body,vms);
});